<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    代理（Proxy）是 JavaScript 中非常强大的功能，它有很多实用的用途：
  </body>
  <script>
    const validator = {
      set(target, key, value) {
        if (key === "age") {
          if (typeof value !== "number" || value < 0) {
            console.log("年龄必须是正数--validator");
          }
        }
        if (key === "email" && !value.includes("@")) {
          throw new Error("邮箱格式不正确");
        }
        target[key] = value;
        return true;
      },
    };

    const person = new Proxy({}, validator);
    person.age = 25; // 正常
    person.age = -5; // 报错：年龄必须是正数

    const validator1 = {
      set(target, key, value) {
        if (key == "age") {
          if (typeof value !== "number" || value < 0) {
            console.log("年龄必须是正数--validator1");
          }
        }
        target[key] = value;
        return true;
      },
    };

    const personMan = new Proxy({}, validator1);
    personMan.age = 25; // 正常
    personMan.age = -5; // 报错：年龄必须是正数

    personMan.name = 'peter'
    personMan.name = '彼得'
  </script>
</html>
